package cn.fintecher.pangolin.service.dataimp.request;

import cn.fintecher.pangolin.common.enums.BaseSelect;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;
import java.util.Objects;

/**
 * @Auther: xiaqun
 * @Description: 待分配案件查询参数
 * @Date: 18:01 2019/9/2
 */

@Data
public class FindDistributeCaseRequest {
    @ApiModelProperty(value = "委托方ID")
    private String principalId;

    @ApiModelProperty(value = "批次号")
    private String batchNumber;

    @ApiModelProperty(value = "客户姓名")
    private String personalName;

    @ApiModelProperty(value = "证件号")
    private String certificateNo;

    @ApiModelProperty(value = "移动电话")
    private String selfPhoneNo;

    @ApiModelProperty(value = "委案阶段")
    private String stage;

    @ApiModelProperty(value = "最小委案金额")
    private Double minOverdueAmtTotal;

    @ApiModelProperty(value = "最大委案金额")
    private Double maxOverdueAmtTotal;

    @ApiModelProperty(value = "最小欠款金额")
    private Double minLeftAmt;

    @ApiModelProperty(value = "最大欠款金额")
    private Double maxLeftAmt;

    @ApiModelProperty(value = "工作单位")
    private String employerName;

    @ApiModelProperty(value = "地址信息")
    private String address;

    @ApiModelProperty(value = "最小委案日期")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date minDelegationDate;

    @ApiModelProperty(value = "最大委案日期")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date maxDelegationDate;

    @ApiModelProperty(value = "最小逾期期数")
    private Integer minOverduePeriods;

    @ApiModelProperty(value = "最大逾期期数")
    private Integer maxOverduePeriods;

    @ApiModelProperty(value = "最小逾期天数")
    private Integer minOverdueDays;

    @ApiModelProperty(value = "最大逾期天数")
    private Integer maxOverdueDays;

    @ApiModelProperty(value = "卡号")
    private String cardNo;

    @ApiModelProperty(value = "委案城市")
    private String city;

    @ApiModelProperty(value = "最小退案日期")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date minEndCaseDate;

    @ApiModelProperty(value = "最大退案日期")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date maxEndCaseDate;

    @ApiModelProperty(value = "案件编号")
    private String caseNumber;

    public BoolQueryBuilder getBuilder() {
        BoolQueryBuilder builder = new BoolQueryBuilder();
        builder.must(QueryBuilders.matchPhraseQuery("isClear.keyword", BaseSelect.NO.name()));
        builder.must(QueryBuilders.matchPhraseQuery("isDistribute.keyword", BaseSelect.NO.name()));
        if (Objects.nonNull(this.principalId)) {
            builder.must(QueryBuilders.matchPhraseQuery("principalId.keyword", this.principalId));
        }
        if (StringUtils.isNotBlank(this.batchNumber)) {
            builder.must(QueryBuilders.wildcardQuery("batchNumber.keyword", "*" + StringUtils.trim(this.batchNumber) + "*"));
        }
        if (StringUtils.isNotBlank(this.personalName)) {
            builder.must(QueryBuilders.wildcardQuery("personalName.keyword", "*" + StringUtils.trim(this.personalName) + "*"));
        }
        if (StringUtils.isNotBlank(this.certificateNo)) {
            builder.must(QueryBuilders.wildcardQuery("certificateNo.keyword", "*" + StringUtils.trim(this.certificateNo) + "*"));
        }
        if (StringUtils.isNotBlank(this.selfPhoneNo)) {
            builder.must(QueryBuilders.wildcardQuery("phone.keyword", "*" + StringUtils.trim(this.selfPhoneNo) + "*"));
        }
        if (StringUtils.isNotBlank(this.caseNumber)) {
            builder.must(QueryBuilders.wildcardQuery("caseNumber.keyword", "*" + StringUtils.trim(this.caseNumber) + "*"));
        }
        if (StringUtils.isNotBlank(this.stage)) {
            builder.must(QueryBuilders.wildcardQuery("stage.keyword", "*" + StringUtils.trim(this.stage) + "*"));
        }
        if (Objects.nonNull(this.minOverdueAmtTotal)) {
            builder.must(QueryBuilders.rangeQuery("overdueAmtTotal").gte(this.minOverdueAmtTotal));
        }
        if (Objects.nonNull(this.maxOverdueAmtTotal)) {
            builder.must(QueryBuilders.rangeQuery("overdueAmtTotal").lte(this.maxOverdueAmtTotal));
        }
        if (Objects.nonNull(this.maxLeftAmt)) {
            builder.must(QueryBuilders.rangeQuery("leftAmt").lte(this.maxLeftAmt));
        }
        if (Objects.nonNull(this.minLeftAmt)) {
            builder.must(QueryBuilders.rangeQuery("leftAmt").gte(this.minLeftAmt));
        }
        if (StringUtils.isNotBlank(this.employerName)) {
            builder.must(QueryBuilders.wildcardQuery("employerName.keyword", "*" + StringUtils.trim(this.employerName) + "*"));
        }
        if (StringUtils.isNotBlank(this.address)) {
            builder.must(QueryBuilders.boolQuery().should(QueryBuilders.wildcardQuery("homeAddr.keyword", "*" + StringUtils.trim(this.address) + "*"))
                    .should(QueryBuilders.wildcardQuery("residenceAddr.keyword", "*" + StringUtils.trim(this.address) + "*"))
                    .should(QueryBuilders.wildcardQuery("employerAddr.keyword", "*" + StringUtils.trim(this.address) + "*"))
                    .should(QueryBuilders.wildcardQuery("billAddr.keyword", "*" + StringUtils.trim(this.address) + "*")));
        }
        if (Objects.nonNull(this.minDelegationDate)) {
            builder.must(QueryBuilders.rangeQuery("delegationDate").gte(this.minDelegationDate.getTime()));
        }
        if (Objects.nonNull(this.maxDelegationDate)) {
            builder.must(QueryBuilders.rangeQuery("delegationDate").lte(this.maxDelegationDate.getTime()));
        }
        if (Objects.nonNull(this.minOverduePeriods)) {
            builder.must(QueryBuilders.rangeQuery("overduePeriods").gte(this.minOverduePeriods));
        }
        if (Objects.nonNull(this.maxOverduePeriods)) {
            builder.must(QueryBuilders.rangeQuery("overduePeriods").lte(this.maxOverduePeriods));
        }
        if (Objects.nonNull(this.minOverdueDays)) {
            builder.must(QueryBuilders.rangeQuery("overdueDays").gte(this.minOverdueDays));
        }
        if (Objects.nonNull(this.maxOverdueDays)) {
            builder.must(QueryBuilders.rangeQuery("overdueDays").lte(this.maxOverdueDays));
        }
        if (StringUtils.isNotBlank(this.cardNo)) {
            builder.must(QueryBuilders.wildcardQuery("cardNo.keyword", "*" + StringUtils.trim(this.cardNo) + "*"));
        }
        if (StringUtils.isNotBlank(this.city)) {
            builder.must(QueryBuilders.wildcardQuery("city.keyword", "*" + StringUtils.trim(this.city) + "*"));
        }
        if (Objects.nonNull(this.minEndCaseDate)) {
            builder.must(QueryBuilders.rangeQuery("endCaseDate").gte(this.minEndCaseDate.getTime()));
        }
        if (Objects.nonNull(this.maxEndCaseDate)) {
            builder.must(QueryBuilders.rangeQuery("endCaseDate").lte(this.maxEndCaseDate.getTime()));
        }
        return builder;
    }
}
